/**
 * @file    soh_calc.c
 * @brief   服务层-计算SOP值
 * @author  name
 * @version 1.0.1
 * @date    2023-04-28
 * 
 * @copyright Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD
 * 
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version   <th>Author   <th>Description
 * <tr><td>2023-04-28 <td>1.0.1     <td>Liuwei   <td>首次创建
 * </table>
 */

/* includes ------------------------------------------------------------------*/
#include "soh_calc.h"
#include "param_mng.h"
#include "cfg_prj.h"
/* macro/define --------------------------------------------------------------*/

/* typedef -------------------------------------------------------------------*/

/* local functions -----------------------------------------------------------*/

/* static variables ----------------------------------------------------------*/

/* global variables ----------------------------------------------------------*/

/** ------------------------------------------------------------------------- *
  *                             Global function
 ** ------------------------------------------------------------------------- */
void soh_calc(void)
{
    uint16_t now_cycle = 0;
    uint16_t base_cycle = 0;
    uint16_t now_soh = 0;
    uint16_t damp_coeff = 0;
	uint16_t full_cap = 0;
	float damp_val = 0;
	
    now_cycle = param_get_data(PAR_ID_CYCLE);
    base_cycle = param_get_data(PAR_ID_SOH_ST_CYCLE);
    damp_coeff = param_get_data(PAR_ID_DAMP_FACTOR);
    now_soh = param_get_data(PAR_ID_SOH);
    if(now_cycle > base_cycle)
    {
		if((now_cycle - base_cycle >= damp_coeff) && (now_cycle > SOH_CYCLE_BASE))
		{
			damp_val = (now_cycle - base_cycle) / damp_coeff;
			// LW** 添加变化数小于2%才处理，否则判定为异常 不进行处理
			if(damp_val <= 2)
				now_soh -= damp_val;
			else
				now_soh -= 1;
			
			base_cycle +=  damp_val * damp_coeff;
			param_set_data(PAR_ID_SOH_ST_CYCLE , base_cycle);
			
			if(PARAM_SOH_IS_VAILD(now_soh))
			{
				param_set_data(PAR_ID_SOH , now_soh);
				full_cap = param_get_data(PAR_ID_STD_CAP) * now_soh * 0.01;	//增加循环次数衰减满充容量	
				param_set_data(PAR_ID_FULL_CAP , full_cap);
			}
		}
    }
    else
    {
        base_cycle = now_cycle;
        param_set_data(PAR_ID_SOH_ST_CYCLE , base_cycle);
    }
}

void soh_set_full_cap(uint16_t full_cap)
{
    uint16_t soh = 0;
    soh = 100 * full_cap / param_get_data(PAR_ID_STD_CAP);
    param_set_data(PAR_ID_SOH_ST_CYCLE , param_get_data(PAR_ID_CYCLE));
    param_set_data(PAR_ID_SOH , soh);
    param_set_data(PAR_ID_FULL_CAP , full_cap);
}

void soh_set_val(uint16_t soh)
{
    uint16_t full_cap = 0;
    
    if(PARAM_SOH_IS_VAILD(soh))
    {
		param_set_data(PAR_ID_SOH , soh);
		full_cap = param_get_data(PAR_ID_STD_CAP) * soh * 0.01;
		param_set_data(PAR_ID_FULL_CAP , full_cap);
	}
}

void soh_set_cycle(uint16_t set_cycle)
{
	uint16_t set_soh = 0;
	uint16_t damp_coeff = 0;
	float cycle_coeff = 0;
	uint16_t full_cap = 0;
	
	if(set_cycle >= SOH_CYCLE_MAX)
		set_cycle = SOH_CYCLE_MAX;
	
	damp_coeff = param_get_data(PAR_ID_DAMP_FACTOR);
	cycle_coeff = 1.0 * set_cycle / damp_coeff;
	if(cycle_coeff >= 0.5f)
		set_soh = 100 - set_cycle / damp_coeff;
	else
		set_soh = 100;
	param_set_data(PAR_ID_CYCLE , set_cycle);
    param_set_data(PAR_ID_SOH_ST_CYCLE , set_cycle);
	if(PARAM_SOH_IS_VAILD(set_soh))
	{
		param_set_data(PAR_ID_SOH , set_soh);
		full_cap = param_get_data(PAR_ID_STD_CAP) * set_soh * 0.01;	//增加循环次数衰减满充容量	
		param_set_data(PAR_ID_FULL_CAP , full_cap);		
	}
}
/** ------------------------------------------------------------------------- *
  *                              Local function
 ** ------------------------------------------------------------------------- */
 
/********** Copyright (c) 2023 JBD-Energy Storage Technology Co. LTD *********/


